package offer;

import common.TreeNode;

/**
 * @author fance
 * @date 18-4-15 上午9:48
 */
public class Chapter4 {

    /**
     * 将二叉树翻转为镜像二叉树
     * @param root
     */
    public void MirrorRecurively(TreeNode root) {
        if (root == null) {
            return;
        }
        if (root.left == null && root.right == null) {
            return;
        }

        TreeNode node = root.left;
        root.left = root.right;
        root.right = node;

        if (root.left != null) {
            MirrorRecurively(root.left);
        }
        if (root.right != null) {
            MirrorRecurively(root.right);
        }
    }

    /**
     * 是否为对称二叉树
     * @param root
     * @return
     */
    public boolean isSymmetrical(TreeNode root) {
        return isSymmetrical(root.left, root.right);
    }
    private boolean isSymmetrical(TreeNode node1, TreeNode node2) {
        if (node1 == null && node2 == null) {
            return true;
        }
        if (node1 == null || node2 == null) {
            return false;
        }
        if (node1.val != node2.val) {
            return false;
        }
        return isSymmetrical(node1.left, node2.right) && isSymmetrical(node1.right, node2.left);

    }



}
